<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" title="编辑佣金" width="800px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="150px">
      <el-form-item label="佣金金额:" prop="commission" >
        <el-input v-model="form.operatorCommission" disabled/>
      </el-form-item>
      <el-form-item label="预估佣金：" prop="commission">
        <div>团长佣金<el-input v-model="form.commission" style="width: 30%" placeholder="请输入" /></div>
        <div style="color: #99a9bf">自动计算，支持手动修改，佣金=佣金总金额 * 佣金比例；</div>
      </el-form-item>
      <el-form-item label="所属团队:" prop="operator_list">
        <el-button size="small" type="primary" style="margin-bottom: 10px;" @click="add">添加所属团队</el-button>
        <el-table
          v-if="form.team_list && form.team_list.length > 0"
          ref="operatorTable"
          :data="form.team_list"
          size="small"
          style="width: 100%;">
          <el-table-column label="团队名称" prop="team_name"/>
          <el-table-column label="团队负责人" prop="leader_name" />
          <el-table-column label="操作" align="center" width="240">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="del(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="cancel">取消</el-button>
      <el-button :loading="addLoading" type="primary" size="small" @click="doSubmit">确认</el-button>
    </div>
    <team-mode ref="teamMode" @callBackAdd="callBackAdd"/>
  </el-dialog>
</template>

<script>
import { updateCommodityTeamInfo, getCommodityTeamInfo } from '@/api/mall/cardCommodityList'
import teamMode from './teamMode'
export default {
  components: { teamMode },
  data() {
    return {
      dialog: false,
      addLoading: false,
      selectCommodityList: [],
      form: {
        operatorCommission: 0, // 省长佣金
        commodity_id_list: [],
        operator_id: 0,
        commission: '', // 团长佣金
        team_list: []
      },
      labelList: [], // 公司标签数据，数据源
      selectList: [], // 选中数据的集合
      commodityLabels: [], // 传递过来的商品的标签
      rules: {
      }
    }
  },
  created() {
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    add() {
      // 获取团队信息
      this.$refs.teamMode.dialog = true
    },
    callBackAdd(data) {
      if (!data || data.length === 0) return
      // 遍历需要添加的数据，往现有数据中添加
      for (const item of data) {
        // 判断字，能否往现有数据中添加内容
        let isAdd = true
        // 遍历现有数据，和新增的数据做对比，若在现有数据已存在，则不做任何处理
        for (const info of this.form.team_list) {
          if (item.team_id === info.team_id) {
            isAdd = false
            break
          }
        }
        // 现有数据中是否存在当前内容，没有则添加，有不做任何处理
        if (isAdd) {
          this.form.team_list.push(item)
        }
      }
    },
    del(row) {
      // 删除
      const index = this.getIndex(row, this.form.team_list)
      this.form.team_list.splice(index, 1)
    },
    getCommodityTeamInfo(item) {
      // 获取复选框，第一个商品所在团队，进行回显
      getCommodityTeamInfo(this.selectCommodityList[0].commodity_id, this.form.operator_id).then(res => {
        // 团队集合
        if (res.list) {
          this.form.team_list = res.list
        }
        // item为默认的团队佣金
        if (res.team_total) {
          this.form.commission = res.team_total > 0 ? res.team_total : item
        } else {
          this.form.commission = item
        }
      })
    },
    getIndex(info, list) {
      // 获取下标
      let num = 0
      // 获取当前list的位置
      list.map((item, i) => {
        if (item.team_id === info.team_id) {
          num = i
        }
      })
      return num
    },
    getCheckToList() {
    },
    doSubmit() {
      const reg = /^\d+(\.\d+)?$/
      if (!reg.test(this.form.commission)) {
        this.$notify({
          title: '输入内容必须为数字',
          type: 'warning',
          duration: 2500
        })
        return
      }
      // 设置的团队佣金大于省长佣金，不能进行保存
      if (this.form.operatorCommission < this.form.commission) {
        this.$notify({
          title: '团长佣金不得大于省长佣金',
          type: 'warning',
          duration: 2500
        })
        return
      }
      this.addLoading = true
      const array = []
      // 合并商品+团队信息
      this.form.team_list.forEach(info => {
        this.selectCommodityList.forEach(item => {
          const i = Object.assign({}, item)
          // 团队id
          i.team_id = info.team_id
          // 团队佣金
          i.team_total = this.form.commission
          // 创建人id，一般指省长id
          i.operator_id = info.create_id
          console.log('info.create_id', info.create_id)
          array.push(i)
        })
      })
      // 团队关联商品表集合
      this.form.team_commodity_list = array
      // 更新团队信息
      updateCommodityTeamInfo(this.form).then(res => {
        this.addLoading = false
        if (res) {
          this.$notify({
            title: '更新成功',
            type: 'success',
            duration: 2500
          })
          this.resetForm()
          // 刷新页面
          this.$parent.init()
        } else {
          this.$notify({
            title: '更新失败',
            type: 'error',
            duration: 2500
          })
        }
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }

</style>
